using System;
using System.Collections.Generic;
using Domain.Entities;
using Domain.Repositories;
using Services;

namespace Logic
{
    public class EditorService:IEditorService
    {
        private ICourseRepository courseRepository;
        private ITopicRepository topicRepository;
        private IChapterRepository chapterRepository;
        private IQuestionRepository questionRepository;
        private IAnswerRepository answerRepository;

        public EditorService(ICourseRepository courseRepository, ITopicRepository topicRepository, IChapterRepository chapterRepository, IQuestionRepository questionRepository, IAnswerRepository answerRepository)
        {
            this.courseRepository = courseRepository;
            this.answerRepository = answerRepository;
            this.topicRepository = topicRepository;
            this.chapterRepository = chapterRepository;
            this.questionRepository = questionRepository;
        }
        public Chapter EditChapter(int? id)
        {
            if (id != null)
            {
                return chapterRepository.Get((int)id);
            }

            return null;
        }

        public Topic EditTopic(int? id)
        {
            if (id != null)
            {
                return topicRepository.Get((int)id);
            }

            return null;
        }

        public Course EditCourse(int? id)
        {
            if (id != null)
            {
                return courseRepository.Get((int) id);
            }
            
            return null;
        }

        public Question EditQuestion(int? id)
        {
            if (id != null)
            {
                return questionRepository.Get((int)id);
            }

            return null;
        }

        public IEnumerable<Question> ViewQuestionForChapter(Chapter chapter)
        {
            return questionRepository.GetListOfQuestionsForChapter(chapter);
        }

        public IEnumerable<Course> GetAllCourses()
        {
            return courseRepository.GetAll();
        }

        public void SaveChapter(Chapter chapter)
        {
            chapterRepository.Save(chapter);
        }

        public void SaveTopic(Topic topic)
        {
            topicRepository.Save(topic);
        }

        public void SaveChapter(int chapterId, string chapterName, string chapterData, int topicId, int numberInOrder)
        {
            Chapter chapter = EditChapter(chapterId)?? new Chapter();
            chapter.ChapterName = chapterName;
            chapter.ChapterData = chapterData;
            chapter.Topic = topicRepository.Get(topicId);
            chapter.NumberInOrder = numberInOrder;
            chapterRepository.Save(chapter);
        }

        public void SaveTopic(int topicId, string topicName, int courseId, int numberInOrder)
        {
            Topic topic = EditTopic(topicId) ?? new Topic();
            topic.TopicName = topicName;
            topic.Course = courseRepository.Get(courseId);
            topic.NumberInOrder = numberInOrder;
            topicRepository.Save(topic);
        }

        public void SaveCourse(int id, string courseName, int numberInOrder)
        {
            Course course = EditCourse(id) ?? new Course();
            course.CourseName = courseName;
            course.NumberInOrder = numberInOrder;
            courseRepository.Save(course);
        }

        public void SaveQuestion(int id, string content, int chapterId, IEnumerable<Answer> answers)
        {
            Question question = EditQuestion(id) ?? new Question();
            question.MediaData = content;
            question.Chapter = chapterRepository.Get(chapterId);
            foreach (var answer in answers)
            {
                answerRepository.Save(answer);
            }
            question.Answers = answers;
            questionRepository.Save(question);
        }
    }
}